package com.daxia.ogmall.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daxia.ogmall.dao.TimelineCommentDAO;
import com.daxia.ogmall.dto.TimelineCommentDTO;
import com.daxia.ogmall.model.TimelineComment;
import com.daxia.ogmall.support.Page;
import com.daxia.ogmall.util.BeanMapper;

@Service
public class TimelineCommentService {
	
	@Autowired
	private TimelineCommentDAO timelineCommentDAO;
	
	public List<TimelineCommentDTO> find(TimelineCommentDTO query, Page page) {
		List<TimelineComment> models = timelineCommentDAO.find(query, page);
		List<TimelineCommentDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	public List<TimelineCommentDTO> findAll() {
		return this.find(new TimelineCommentDTO(), null);
	}
	
	public List<TimelineCommentDTO> find(int count) {
		Page page = new Page();
		page.setPage(1);
		page.setPageSize(count);
		return this.find(new TimelineCommentDTO(), page);
	}
	
	public Long create(TimelineCommentDTO dto) {
		TimelineComment model = new TimelineComment();
		toModel(model, dto);
		return timelineCommentDAO.create(model);
	}
	
	public TimelineCommentDTO load(Long id) {
	    TimelineComment model = timelineCommentDAO.load(id);
	    return toDTO(model);
    }

	public void updateAllFields(TimelineCommentDTO dto) {
		TimelineComment model = timelineCommentDAO.load(dto.getId());
		toModel(model, dto);
		timelineCommentDAO.update(model);
    }

	public void deleteByIds(Long[] ids) {
		if (ids != null) {
			for (Long id : ids) {
				timelineCommentDAO.deleteById(id);
			}
		}
    }
	
	public void deleteById(Long id) {
		this.deleteByIds(new Long[] {id});
	}

	public TimelineCommentDTO findOne(TimelineCommentDTO query) {
		TimelineComment model = timelineCommentDAO.findOne(query);
		return toDTO(model);
	}
	
	private List<TimelineCommentDTO> toDTOs(List<TimelineComment> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<TimelineCommentDTO>(0);
		}
		List<TimelineCommentDTO> dtos = new ArrayList<TimelineCommentDTO>(models.size());
		for (TimelineComment model : models) {
	        TimelineCommentDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
    
    private TimelineCommentDTO toDTO(TimelineComment model) {
		if (model == null) {
			return null;
		}
		TimelineCommentDTO dto = BeanMapper.map(model, TimelineCommentDTO.class);
		
		return dto;
	}
	
	private void toModel(TimelineComment model, TimelineCommentDTO dto) {
		BeanMapper.copy(dto, model);
    }
	
	@SuppressWarnings("unused")
	private List<TimelineComment> toModels(List<TimelineCommentDTO> dtos) {
		if (CollectionUtils.isEmpty(dtos)) {
			return new ArrayList<TimelineComment>(0);
		}
		List<TimelineComment> models = new ArrayList<TimelineComment>(dtos.size());
		for (TimelineCommentDTO dto : dtos) {
	        TimelineComment model = new TimelineComment();
	        toModel(model, dto);
	        models.add(model);
        }
		return models;
	}
}
